{include file="header/index" /}
<div class="newContentBox">

    <form class="layui-form newListPart" id="myform" enctype="multipart/form-data">
        <input type="hidden" id="id" value="{$id}">
        <div class="newPart">
            <!-- 基本信息 -->
            <div class="newTitleBox">
                <span class="layui-inline newListTitle">基本信息</span>
                <a class="newBtnDefault newBtnGray" onclick="returnBack(this)"> <i class="fa fa-mail-reply"></i>返回</a>
            </div>
            <div class="newContent">

                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l"><span class="cover_new_label">*</span>合同附件</label>
                    <div class="layui-input-block cover_layui_i_b layui-upload">
                        <button type="button" class="layui-btn newBtnPrimary" id="uploadMore">上传文件</button>
                        <div class="layui-upload-list">
                            <div>
                                <!-- 隐藏域 轮播图 -->
                                <!-- <input type="hidden" name="shuffling_pic[]"  value="" class="layui-input">
                                 <input type="hidden" name="original_file[]"  value="" class="layui-input">
                                 <input type="hidden" name="file_type[]"  value="" class="layui-input">-->
                                <!--<div></div>
                                <div class="remove" onclick="removeFile(this)"><i class="fa fa-times"></i></div>-->
                            </div>
                        </div>
                        <span class="redcolor">备注：支持图片或文件上传。至少上传一张</span>
                    </div>
                </div>
            </div>
            <div class="layui-form-item ">
                <div class="layui-input-block cover_layui_i_b">
                    <a class="layui-btn newBtnDefault" onclick="javascript:history.go(-1);">取消</a>
                    <a class="layui-btn newBtnDefault newBtnPrimary" id="btn">确定</a>
                </div>
            </div>
        </div>

    </form>

</div>

<script>
    var layer = layui.layer,
        element = layui.element,
        laydate = layui.laydate,
        laypage = layui.laypage,
        form = layui.form,
        upload = layui.upload,
        layedit = layui.layedit,
        laytpl = layui.laytpl;
    form.render();

    // 多文件上传--合同附件
    var uploadMore = upload.render({
        elem: '#uploadMore',
        accept: 'image/*',
        type:'images',
        url: "{:url('commonapi/upload/uploadImg')}",
        multiple: true,
        number: 5,
        done: function (res) {
            //如果上传失败
            if (res.code != 1000) {
                return layer.msg('上传失败', {icon: 2,time:1000});
            } else {
                parent.layer.msg('上传成功',{icon:1,time:1000});
                var html = '<div>';
                html += '<div>'+res.data.original_name+'</div>';
                html += '<div class="remove" onclick="removeFile(this)"><i class="fa fa-times"></i></div>';
                html += '<input type="hidden" name="shuffling_pic[]"  value="' + res.data.url + '">';
                html += '<input type="hidden" name="original_file[]"  value="' + res.data.original_name + '">';
                html += '<input type="hidden" name="file_type[]"  value="' + res.data.type + '">';
                html += '</div>';
                $("#uploadMore").siblings('.layui-upload-list').append(html);
            }
            //上传成功
        },
        error: function () {
            layer.msg('上传失败', {
                icon: 2,
                time:1000
            })
        }
    });

    //提交表单
    $("#btn").click(function () {
        //上传后的文件名
        var shuffling_pic = [];
        $("input[name='shuffling_pic[]']").each(function (index, el) {
            shuffling_pic.push($(this).val());
        });
        //原文件名
        var original_file = [];
        $("input[name='original_file[]']").each(function (index, el) {
            original_file.push($(this).val());
        });
        //文件类型
        var file_type = [];
        $("input[name='file_type[]']").each(function (index, el) {
            file_type.push($(this).val());
        });

        var index = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        var request_data = {};
        request_data.id = $("#id").val();
        request_data.shuffling_pic =shuffling_pic;
        request_data.original_file = original_file;
        request_data.file_type = file_type;

        request_data = JSON.stringify(request_data);
        $.post("{:config('api')}/Achievement/Dealer/uploadDealerContract",request_data,function (flag) {
            layer.close(index);
            if (flag.code < 0){
                layer.msg(flag.message,{icon:2,time:1500,shade: 0.1,});
                return false;
            }
            layer.msg(flag.message,{icon:1,time:1500,shade: 0.1,});
            window.location.reload();
        })
    });


    $(function(){
        // 焦点事件验证数据是否为空
        $("input[name='shuffling_pic[]']").blur(function(){
            var shuffling_pic = $("input[name='shuffling_pic[]']").val();
            if (shuffling_pic == '') {
                top.layer.msg('请上传合同', {icon: 2});
                return false;
            }
        });

    })
</script>
